const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const { getPlugins } = require('./utils/plugins');
const { PRODUCTION, ROOT_PATH, SRC_PATH, DIST_PATH, getCDNPath } = require('./utils/variables');

module.exports = {
  entry: {
    index: path.join(SRC_PATH, 'index.tsx'),
  },
  output: {
    path: DIST_PATH,
    filename: PRODUCTION
      ? 'assets/js/[name].[contenthash:8].bundle.js'
      : 'assets/js/[name].bundle.js',
    publicPath: getCDNPath(),
    globalObject: 'this',
    chunkFilename: PRODUCTION
      ? 'assets/js/[name].[contenthash:8].chunk.js'
      : 'assets/js/[name].chunk.js',
    assetModuleFilename: 'assets/[hash][ext][query]',
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.(tsx?|jsx?)$/,
        include: [SRC_PATH],
        use: ['happypack/loader?id=babel'],
      },
      {
        test: /\.css$/i,
        use: [
          !PRODUCTION ? 'style-loader' : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: false,
              sourceMap: !PRODUCTION,
            },
          },
          'postcss-loader',
        ],
      },
      {
        test: /(\.scss|\.sass)$/i,
        include: [SRC_PATH],
        use: [
          !PRODUCTION ? 'style-loader' : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: false,
              sourceMap: !PRODUCTION,
            },
          },
          'postcss-loader',
          'sass-loader',
          {
            loader: 'style-resources-loader',
            options: {
              patterns: path.resolve(SRC_PATH, 'assets', 'styles', 'common', 'index.scss'),
            },
          },
        ],
      },
      {
        test: /\.less$/i,
        include: [SRC_PATH],
        use: [
          !PRODUCTION ? 'style-loader' : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: false,
              sourceMap: !PRODUCTION,
            },
          },
          'postcss-loader',
          {
            loader: 'less-loader',
            options: {
              sourceMap: true,
              lessOptions: {
                modifyVars: {},
              },
            },
          },
          {
            loader: 'style-resources-loader',
            options: {
              patterns: path.resolve(SRC_PATH, 'assets', 'styles', 'common', 'index.less'),
            },
          },
        ],
      },
      {
        test: /\.(png|jpg|gif|jpeg|webp|svg)$/,
        type: 'asset/resource',
        generator: {
          filename: 'assets/images/[hash][ext][query]',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'assets/fonts/[hash][ext][query]',
        },
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
    modules: [path.resolve(ROOT_PATH, 'node_modules')],
    // 查找 package.json main
    mainFields: ['main'],
    alias: {
      '@': SRC_PATH,
      '@@': ROOT_PATH,
      pages: path.resolve(SRC_PATH, 'pages'),
    },
  },
  plugins: getPlugins(),
};
